package com.limao.servlet;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;

import com.limao.CacheManager.UploadInfo;
import com.limao.Model.LzPluginException;
import com.limao.Model.PublicData;
import com.limao.Tool.CacheClass;
import com.limao.Utils.OutPrintClass;

public class uploadAsyncProcessor  implements Runnable{


	private AsyncContext asyncContext;
	private HttpServletRequest request;
	private HttpServletResponse response;

	public uploadAsyncProcessor() {
	}

	public uploadAsyncProcessor(AsyncContext asyncCtx, HttpServletRequest req,HttpServletResponse response) {
		this.asyncContext = asyncCtx;
		this.request = req;
		this.response=response;
	}

	@Override
	public void run() {

		Object reply = null;
		try {
			
			String userid=CacheClass.isLogin(request,response);
			
			if(CacheClass.isEmpty(userid))
			{
				PublicData jsonObj = new PublicData();
				jsonObj.state = "100";
				jsonObj.info = "您还未登陆";
				OutPrintClass.writer(response, jsonObj,asyncContext);
				return;
			}
			
			
			if(CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("ispaused"))==1)
			{
				OutPrintClass.sendError(response, asyncContext, -1, "系统维护中");
				return ;
			}
			
			
			reply = UploadInfo.UploadFile(request, asyncContext);
			
			
		} catch (LzPluginException e) {
			reply = OutPrintClass.errorObj(-1,e.getMessage());
		}catch(FileUploadException e)
		{
			reply = OutPrintClass.errorObj(-1,"文件过大或格式不正确");

		} catch (Exception e) {
			reply = OutPrintClass.errorObj(-1, "出错，未知错误:uploadAsyncProcessor");
		}
		
		OutPrintClass.writer(response, reply, asyncContext);
	}


}
